package com.huangpuweb.api.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.*;

/**
 * created by Intellij IDEA
 *
 * @author Chengzhifeng
 * @date 2020/4/26
 * @statement
 */
public class Test {


    //读取json文件
    public static String readJsonFile(String fileName) {
        File file = new File("F:/" + fileName);
        StringBuilder localStrBulider = new StringBuilder();
        if (file.isFile() && file.exists()) {
            try {
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader bufferReader = new BufferedReader(inputStreamReader);
                String lineStr = null;
                try {
                    while ((lineStr = bufferReader.readLine()) != null) {
                        localStrBulider.append(lineStr);
                    }
                    bufferReader.close();
                    inputStreamReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("file read error!");
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                System.out.println("file catch unsupported encoding!");
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("file not found!");
                e.printStackTrace();
            }
        } else {
            System.out.println("file is not a file or file is not existing!");
            return null;
        }
        return localStrBulider.toString();
    }

    public static void main(String[] args) throws Exception {
        try {


            HttpClient httpclient = new DefaultHttpClient();


            HttpPost httpPost = new HttpPost("http://172.82.4.49:9999/ecis-access/api/event/getLatestFiveEvent");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key", "XJYJ_2020_XZJSC");
            StringEntity s = new StringEntity(jsonObject.toString());
            s.setContentEncoding("utf-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            httpPost.setEntity(s);
            HttpResponse res = httpclient.execute(httpPost);

            //            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//            nvps.add(new BasicNameValuePair("key", "XJYJ_2020_XZJSC"));
////            nvps.add(new BasicNameValuePair("password", "secret"));
//            httpPost.setEntity(new UrlEncodedFormEntity(nvps));


//            HttpResponse response = httpclient.execute(httpPost);
            String content = EntityUtils.toString(res.getEntity());
            System.out.println(content);
            httpclient.getConnectionManager().shutdown();


//            String url = "http://172.82.4.49:9999/ecis-access/api/event/getLatestFiveEvent";
//
//            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
//            parameters.add(new BasicNameValuePair("key", "XJYJ_2020_XZJSC"));
//
//            HttpClient httpClient = new DefaultHttpClient();
//            HttpPost post=new HttpPost(url);
////            post.set
//            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
//            // 将请求实体设置到httpPost对象中
//            post.setEntity(formEntity);
//            HttpResponse response = httpClient.execute(post);
//            String content= EntityUtils.toString(response.getEntity());
//            System.out.println(content);
        } catch (Exception e) {
            System.out.println("e----------" + e);
        }


    }
}
